import { Component, ViewChild, OnInit } from '@angular/core';
import { ServerDataSource } from 'ng2-smart-table';
// api
import { BasicService } from '../../../@core/data/basic.service';
// import { Observable } from 'rxjs/Rx';
import { Http } from '@angular/http';
// 消息提醒
import { ToasterConfig } from 'angular2-toaster';
import { NotificationsService } from '../../../@core/utils/notifications.service';
// confirm
import { SwalComponent } from '@toverux/ngx-sweetalert2';
// router
import { Router } from '@angular/router';
const api = 'papers';
@Component({
  selector: 'ngx-smart-table-mypaper',
  templateUrl: './mypaper.component.html',
  styles: [`
    nb-card {
      transform: translate3d(0, 0, 0);
    }
  `],
})
export class MyPaperComponent implements OnInit {
  @ViewChild('deleteSwal') private deleteSwal: SwalComponent;

  settings = {
    // selectMode: 'multi', //多选，全选操作未实现
    actions: {
      columnTitle: '操作',
      position: 'right',
      add: false,
      // edit: false,
      delete: false,
    },
    noDataMessage: '暂无数据',
    mode: 'external',
    pager: {
      perPage: 10,
    },
    edit: {
      editButtonContent: '<i class="nb-compose" title="参加考试"></i>',
    },
    columns: {
      // id: {
      //   title: 'ID',
      //   type: 'number',
      //   sort: 'enable',//表内排序
      //   sortDirection: 'desc'
      // },
      courseName: {
        title: '课程',
        // filter: false,
        type: 'string',
      },
      qbankName: {
        title: '题库',
        // filter: false,
        type: 'string',
      },
      title: {
        title: '试卷名称',
        type: 'html',
        // filter: false,
      },
      statusName: {
        title: '状态',
        // editable: false,
        // addable: false,
        // filter: false,
        type: 'string',
      },
    },
  };

  source: ServerDataSource;

  config: ToasterConfig;

  constructor(private service: BasicService, private http: Http,
    private notificationsService: NotificationsService, private router: Router) {
    this.source = new ServerDataSource(http,
      {
        endPoint: this.service.api + api,
        totalKey: this.service.totalKey,
        dataKey: this.service.dataKey,
        sortFieldKey: this.service.sortFieldKey,
        sortDirKey: this.service.sortDirKey,
        pagerPageKey: this.service.pagerPageKey,
        pagerLimitKey: this.service.pagerLimitKey,
        filterFieldKey: this.service.filterFieldKey,
      });
    this.config = this.notificationsService.getConfig();
  }

  ngOnInit() {

  }

  onEdit(event) {
    // 跳转到相关的试卷页面
    // console.log(event.data);
    const paperId = event.data.id;
    this.router.navigate(['/pages/exam/doexam/' + paperId], { skipLocationChange: false });
  }

  onUserRowSelect(event) {
    // console.log(event.data);
  }

}
